package config

import (
	"fmt"
	"strings"
)

type Mysql struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	DB       string `yaml:"db"`
	Config   string `yaml:"config"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
	LogLevel string `yaml:"log_level"`
}

func (m *Mysql) Dsn() string {
	var sb strings.Builder
	sb.WriteString(m.User)
	sb.WriteString(":")
	sb.WriteString(m.Password)
	sb.WriteString("@tcp(")
	sb.WriteString(m.Host)
	sb.WriteString(":")
	sb.WriteString(fmt.Sprintf("%d", m.Port)) // 使用fmt.Sprintf添加错误处理
	sb.WriteString(")/")
	sb.WriteString(m.DB)
	sb.WriteString("?charset=utf8mb4&parseTime=True&loc=Local")
	sb.WriteString(m.Config)
	return sb.String()
}
